<?php 
$weekarr = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$week = $weekarr[date("w")];

$application = $profile['application'];
?>
<style>
.input-div table tr{display:block;}
</style>
<div id="order" class="indexcontentdiv">
    <div class="positiondiv">
        <table>
            <tr>
                <td class="td-left">
                    <img src="<?=base_url()?>resource/img/home_ico.png" width="21" height="17">
                    Profile > <a href="<?=base_url()?>profile/index">Profile</a> >Edit Profile
                </td>
                <td class="td-right">
                    <img src="<?=base_url()?>resource/img/menu2.png" width="27" height="24">Today is <?=$week?>,<?=date('M d,Y')?>
                </td>
            </tr>
        </table>
    </div>
    <div class="input-div">
        <form action="<?=base_url('profile/profile_edit')?>" method="post" onkeydown="if(event.keyCode==13)return false;" onsubmit="return checkform()">
        <table>
            <tr><td colspan="2"><h3>.Edit Profile</h3></td></tr>
            <tr>
                <td class="nametd">Name:</td>
                <td>
                    <?=$profile['name']?>
                    <input type="hidden" name="lastapplication" value="<?=$application?>" />
                    <input type="hidden" name="id" value="<?=$profile['id']?>" />
                </td>
            </tr>
            <tr>
                <td class="nametd">Application:</td>
                <td>
                    <?php if($application*1 == 3){?>
                    <input type="radio" name="application" value="3" id="ReApplication" checked="checked" /><label for="ReApplication">Re Application</label>    
                    <?php }else{?>
                    <input type="radio" name="application" value="1" id="NewApplication" /><label for="NewApplication">New Application</label>
                    <input type="radio" name="application" value="2" id="onbehalf" /><label for="onbehalf">ON Behalf</label>
                    <input type="radio" name="application" value="4" id="malaysian" /><label for="malaysian">New application for malaysian</label>    
                    <?php }?>
                </td>
            </tr>
            <tr class="existorder">
                <td class="nametd">Order:</td>
                <td>
                    <input type="text" name="ordersn" id="ordersn" readonly="readonly" title=""/>
                    <input type="hidden" name="orderid" id="orderid"/>
                    <span id="ordersnmsg" class="err">*</span>
                </td>
            </tr>
            <tr>
                <td class="nametd">Company Name:</td>
                <td>
                    <input type="text" name="name" id="name" readonly="readonly" title=""/>
                    <input type="hidden" name="companyid" id="companyid"/>
                    <span id="companymsg" class="err">*</span>
                </td>
            </tr>
            <?php if($application*1 != 3){?>
            <tr class="workers">
                <td class="nametd">Workers:</td>
                <td>
                    <input type="text" name="workers" id="workers" readonly="readonly"/>
                    <input type="hidden" name="workersid" id="workersid"/>
                    <span id="workersmsg" class="err">*</span>
                </td>
            </tr>
            <?php }?>
            <tr>
                <td class="nametd">Oversea Agent:</td>
                <td>
                    <select style="width:200px;" id="agent" name="agent">
                        <option value="0">--SELECT--
                        <?php foreach ($agent as $key => $value) {?>
                        <option value="<?=$value['id']?>"><?=$value['name']?>
                        <?php }?>
                    </select>
                </td>
            </tr>
            <tr><td class="nametd">Sales Person:</td>
                <td>
                    <select name="saleser" id="saleser" style="width:140px;">
                        <option value="0">-Saleser-
                        <?php foreach ($marketing as $key => $value) {?>
                            <option value="<?=$value['id']?>"><?=$value['username']?>
                        <?php }?>
                    </select>
                </td>
            </tr>
            <tr><td class="nametd">Source:</td>
                <td><input type="text" name="source" value="<?=$profile['source']?>"/><span id="sourcemsg" class="err">*</span></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="Save"/>
                </td>
            </tr>
        </table>
        </form>
    </div>
</div>
<script>
var existorder = '<?=$application?>';
$(function () {
    //init
    $("input[name='application']:eq("+(existorder-1)+")").attr("checked","checked");
    if(existorder*1 == 4){
        $("input[name='application']:eq(2)").attr("checked","checked");
    }
    $("input[name='ordersn']").val("<?=$order['sn']?>");
    $("input[name='orderid']").val("<?=$order['id']?>");
    $("input[name='name']").val("<?=$company['name']?>");
    $("input[name='companyid']").val("<?=$company['id']?>");
    $("input[name='workers']").val("<?=$worker['name']?>");
    $("input[name='workersid']").val("<?=$worker['id']?>");
    $("#agent").val("<?=$worker['companyid']?>");
    $("#saleser").val("<?=$profile['saleser']?>");
    initView();
    
    $("input[name='application']").click(function(){
        $("input[name='application']").each(function(){
            if($(this).attr("checked") == "checked"){
                existorder = $(this).val();
                initView();
            }
        });
    });
});

function initView(){
    if(existorder == 1 || existorder == 3){
        $(".existorder").css("display","block");
        $(".workers").css("display","block");
    }else{
        $(".existorder").css("display","none");
        $(".workers").css("display","block");
    }
}

function checkform(){
    var flag = true;
    var ordersn = $("input[name='ordersn']").val();
    var name = $("input[name='name']").val();
    var workers = $("input[name='workers']").val();
    var profile = $("input[name='profile']").val();
    
    if(name == ''){//公司不存在
        $("#companymsg").html("The company name can not be empty!");
        flag = false;
    }else{
        $("#companymsg").html("*");
    }
    
    if(existorder != 3){
        if(workers == ''){//公司不存在
            $("#workersmsg").html("The workers can not be empty!");
            flag = false;
        }else{
            $("#workersmsg").html("*");
        }
    }
    
    if(existorder == 1 || existorder == 3){//存在订单
        if(ordersn == ''){
            $("#ordersnmsg").html("Order can not be empty!");
            flag = false;
        }else{
            $("#ordersnmsg").html("*");
        }
    }
    
    if(existorder == 3){//profile检查
        if(profile == ''){
            $("#profilemsg").html("Profile can not be empty!");
            flag = false;
        }else{
            $("#profilemsg").html("*");
        }
    }
    
    return flag;
}

var iTop = (document.body.clientHeight-30-600)/2; //获得窗口的垂直位置;
var iLeft = (document.body.clientWidth-10-800)/2; //获得窗口的水平位置;
$("input[name='workers']").click(function(){
    window.open('<?=base_url("profile/choice_workers")?>','_blank',
    'width=800,height=600,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,top='+iTop+',left='+iLeft); 
});

$("input[name='ordersn']").click(function(){
    window.open('<?=base_url("profile/choice_orders")?>','_blank',
    'width=800,height=600,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,top='+iTop+',left='+iLeft); 
});

$("input[name='name']").click(function(){
    window.open('<?=base_url("profile/choice_company")?>','_blank',
    'width=800,height=600,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,top='+iTop+',left='+iLeft); 
});

$("input[name='profile']").click(function(){
    window.open('<?=base_url("profile/choice_profile")?>','_blank',
    'width=800,height=600,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,top='+iTop+',left='+iLeft); 
});

$('#statustime').datetimepicker({
    format:"Y-m-d",      //格式化日期
    timepicker:false,    //关闭时间选项
});
</script>
　